import axios from 'axios';

const request = axios.create({
  baseURL: '/api',
  timeout: 10000
});

// 添加请求拦截器
request.interceptors.request.use(function (config) {
  // 在发送请求之前，从 localStorage 中获取 Token 并设置到请求头中
  const token = sessionStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
}, function (error) {
  return Promise.reject(error);
});

// 添加响应拦截器
request.interceptors.response.use(function (response) {
  return response.data;
}, function (error) {
  return Promise.reject(error);
});

export default request;